package ani.trainning.cacobatph.dac;

import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import ani.trainning.cacobatph.ConstantUtil;
import ani.trainning.cacobatph.utilities;

public class THR4_quittance {

	private static final String N_DAC = "n_Dac";
	private static final String N_COTISANT = "n_Cotisant";
	private static final String C_PERIODE = "c_Periode";
	private static final String C_NATURE = "c_Nature";
	private static final String TAUX = "taux";
	private static final String ASSIETTE = "assiette";
	private static final String DEBIT = "debit";
	private static final String NB_SALARIE = "nb_Salarie";
	private static final String ID_AGENCE = "id_Agence";
	private static final String LIBELLE = "libelle";
	private static final String ETATDAC = "etatDac";
	private static final String CODEOP = "codeOp";

	private final Handler mHandler;
	private final Context context;
	private final List<_DAC> dacs;

	private final String userName;
	private final String passWord;
	private final String url;

	// constructor
	public THR4_quittance(Handler handler, Context context, String user, String pass, List<_DAC> dacs, String _url) {
		mHandler = handler;
		this.context = context;

		this.dacs = dacs;
		this.userName = user;
		this.passWord = pass;
		this.url = _url;
	}

	public Thread createThread() {
		return new Thread() {
			@Override
			public void run() {
				// creation du message.
				try {
					//	context.getResources().getString(R.string.WS02_DAC_METHOD)
					Dictionary<String, String> params = new Hashtable<String, String>();
					params.put(ConstantUtil.KEY_USERNAME, userName);
					params.put(ConstantUtil.KEY_PASSWORD, passWord);
					params.put(ConstantUtil.KEY_DACS, dacToJson(dacs));
					String response = utilities.getContent(url, params);
					
					Message msg=new Message();
					msg.what = ACT3_DACprincipale.ENVOI_REUSSI;
					msg.obj = response;
					mHandler.sendMessage(msg);
				}
				catch (IOException e) {
					Log.e("thread exeption : ", ""+e.getMessage());
					mHandler.sendEmptyMessage(ACT3_DACprincipale.EXEPTION_IO2);
				}
				catch (Exception e) {
					Message msg=new Message();
					msg.what = ACT3_DACprincipale.ECHEC_ENVOI;
					msg.obj = "" + e.getMessage();
					mHandler.sendMessage(msg);
				}

			}
		};
	}

	public String dacToJson(List<_DAC> _dacs) throws Exception{

		JSONArray dacArray = new JSONArray();
		for (_DAC _dac : _dacs) {

			JSONObject dacObject = new JSONObject();

			dacObject.put(N_DAC, _dac.n_Dac);
			dacObject.put(N_COTISANT, _dac.n_Cotisant);
			dacObject.put(C_PERIODE, _dac.c_Periode);
			dacObject.put(C_NATURE, _dac.getC_Nature());

			dacObject.put(TAUX, _dac.getTaux());
			dacObject.put(ASSIETTE, _dac.getAssiette());
			dacObject.put(DEBIT, _dac.getDebit());
			dacObject.put(NB_SALARIE, _dac.getNb_Salarie());

			dacObject.put(ID_AGENCE, _dac.id_Agence);
			dacObject.put(LIBELLE, _dac.getLibelle());
			dacObject.put(ETATDAC, _dac.etatDac);
			dacObject.put(CODEOP, _dac.codeOp);

			dacArray.put(dacObject);
		}
		return dacArray.toString();
	}
}
